package com.hospital;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hospital.pony.base.BaseActivity;
import com.hospital.pony.config.Consts;
import com.hospital.pony.db.MemberUserUtils;
import com.hospital.pony.db.ShowObservable;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.model.UserModel;
import com.hospital.pony.util.TimeUtilDialog;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.view.DialogMsg;

import net.tsz.afinal.http.AjaxParams;

import io.rong.imkit.RongIM;

public class DoctorMessageActivity extends BaseActivity implements TimeUtilDialog.TimePickerDialogInterface{
	// title
	private TextView mTvTitle;
	// 返回
	private ImageView mIvBack;
	// 查询按钮
	private TextView mtvtitle;

	UserModel noticeModel;

	private TextView departmentHosName;
	private TextView doctorName;
	private TextView doctorSex;
	private TextView doctorYear;
	private TextView departmentName;
	private TextView doctorMessage;

	private Button mbtnPay;
	private TextView mtvShopPrice;

	private int choiceTime = 1;
	private DialogMsg dialogMsg;
	private ImageView mivUserImage;
	private Button mbtnIM;


	TimeUtilDialog mTimePickerDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctornewsmsg);
		initWidget();
		initData();
	}

	@Override
	public void initWidget() {

		mTimePickerDialog = new TimeUtilDialog(this);


		dialogMsg = new DialogMsg(this);
		dialogMsg.Set_Msg("预约成功，可在个人中心查看自己的预约信息");

		dialogMsg.submit_ok().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DoctorMessageActivity.this.finish();
				dialogMsg.Close();
			}
		});
		dialogMsg.submit_no().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DoctorMessageActivity.this.finish();
				dialogMsg.Close();
			}
		});

		mbtnPay = (Button) findViewById(R.id.mbtnPay);
		mbtnPay.setOnClickListener(this);
		mivUserImage = (ImageView) findViewById(R.id.mivUserImage);
		departmentHosName = (TextView) findViewById(R.id.departmentHosName);
		doctorName = (TextView) findViewById(R.id.doctorName);
		doctorSex = (TextView) findViewById(R.id.doctorSex);
		doctorYear = (TextView) findViewById(R.id.doctorYear);
		departmentName = (TextView) findViewById(R.id.departmentName);
		doctorMessage = (TextView) findViewById(R.id.doctorMessage);

		mtvShopPrice = (TextView) findViewById(R.id.mtvShopPrice);
		// mtvtitle = (TextView) findViewById(R.id.mtvtitle);

		mIvBack = (ImageView) findViewById(R.id.mIvBack);
		mTvTitle = (TextView) findViewById(R.id.mTvTitle);
		mTvTitle.setText("医生详情");
		mIvBack.setVisibility(View.VISIBLE);
		mIvBack.setOnClickListener(this);

		mbtnIM = (Button) findViewById(R.id.mbtnIM);
		mbtnIM.setOnClickListener(this);
		mtvShopPrice.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		if(v.getId()==R.id.mIvBack){
			DoctorMessageActivity.this.finish();
		}
		if(v.getId()==R.id.mbtnIM){
			RongIM.getInstance().startPrivateChat(this, noticeModel.getUid(), noticeModel.getUname());
		}
		if(v.getId()==R.id.mbtnPay){
			if (mtvShopPrice.getText().toString().equals("请选择时间")) {
				ToastUtil.ShowCentre(this, "请选择时间");
				return;
			}
			addMessage(false);
		}
		if(v.getId()==R.id.mtvShopPrice){
			choiceTime = 1;
			mTimePickerDialog.showDatePickerDialog();
		}
	}



	@Override
	public void initData() {


		noticeModel = (UserModel) this.getIntent().getSerializableExtra("msg");
		departmentHosName = (TextView) findViewById(R.id.departmentHosName);
		doctorName = (TextView) findViewById(R.id.doctorName);
		doctorSex = (TextView) findViewById(R.id.doctorSex);
		doctorYear = (TextView) findViewById(R.id.doctorYear);
		departmentName = (TextView) findViewById(R.id.departmentName);
		doctorMessage = (TextView) findViewById(R.id.doctorMessage);

		departmentHosName.setText(noticeModel.getUname());
		doctorName.setText("职称：" + noticeModel.getULeave());
		doctorSex.setText("性别：" + noticeModel.getUsex());

		departmentName.setText("科室：" + noticeModel.getUdepartName());
		doctorYear.setText("手机：" + noticeModel.getUphone());



		// if (!TextUtils.isEmpty(noticeModel.getDoctorImage())) {
		// Picasso.with(this).load(Consts.URL_IMAGE_LOCAL +
		// noticeModel.getDoctorImage()).placeholder(R.drawable.default_drawable_show_pictrue)
		// .into(mivUserImage);
		// }
//		mtvShopPrice.setText( noticeModel.getUphone());
		// mtvShopPrice.setText("预约数："+noticeModel.getTotalNumber()+"/"+noticeModel.getDoctorNumber());
	}


	private void addMessage(boolean isShow) {

		UserModel userModel = (UserModel) MemberUserUtils.getBean(this, "user_messgae");

		AjaxParams params = new AjaxParams();
		params.put("action_flag", "addBorrow");
		params.put("applyUserId", MemberUserUtils.getUid(this));
		params.put("applyUserName","患者姓名："+userModel.getUname()+"-患者手机号码："+userModel.getUphone());
		params.put("applyMessageId", noticeModel.getUid());
		params.put("applyMessageName", noticeModel.getUname());
		params.put("applyDeptId", noticeModel.getUdepartID());
		params.put("applyTime", mtvShopPrice.getText().toString());
		httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultFlag, isShow, "正在参与...");
	}
	@Override
	protected void callBackSuccess(ResponseEntry entry, int actionId) {
		super.callBackSuccess(entry, actionId);

		switch (actionId) {

			case Consts.actionId.resultFlag:
				ShowObservable.getInstance().notifyStepChange("ok");
				ToastUtil.show(this, "预约成功");
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						finish();
					}
				}, 2000);
				break;
		}

	}

	@Override
	public void positiveListener() {
		String year = mTimePickerDialog.getYear() + "";
		String hour = mTimePickerDialog.getHour() + "";
		String minute = mTimePickerDialog.getMinute() + "";
		String month;
		String day;

		if (mTimePickerDialog.getMonth() < 9) {
			month = "0" + (mTimePickerDialog.getMonth());
		} else {
			month = "" + (mTimePickerDialog.getMonth());
		}
		if (mTimePickerDialog.getDay() < 9) {
			day = "0" + (mTimePickerDialog.getDay());
		} else {
			day = "" + (mTimePickerDialog.getDay());
		}
		if (mTimePickerDialog.getMinute() < 9) {
			minute = "0" + (mTimePickerDialog.getMinute());
		} else {
			minute = "" + (mTimePickerDialog.getMinute());
		}

		if (mTimePickerDialog.getHour() < 9) {
			hour = "0" + (mTimePickerDialog.getHour());
		} else {
			hour = "" + (mTimePickerDialog.getHour());
		}
		if (flag) {

			mTimePickerDialog.showTimePickerDialog();
			if(choiceTime==1){
				mtvShopPrice.setText(year + "-" + month + "-" + day);
			}

		} else {

			if(choiceTime==1){
				mtvShopPrice.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
			}
		}

		flag = false;



	}

	private boolean flag = true;

	@Override
	public void negativeListener() {

	}
}
